<?php

class Check_model extends CI_Model {

    public $_return_result = [];

    function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
        $this->load->model("manage_model", "manage");
        $this->load->model("admin_model", "admin");

        $this->addLog();
    }

    /*
     * 是否登录
     */

    function loginAdminInfo() {

        $outArr = array();

        $admin_id = $this->session->session_admin_user_id;
        if (!empty($admin_id)) {
            $result = $this->admin->getAdminInfoById($admin_id);
            if (!empty($result)) {//如果用户表存在
                $role = $this->admin->getAdminRoleById($result["role_id"]);
                if (!empty($role)) {
                    $result["power"] = $role["power"];
                    $result["menu"] = $role["menu"];
                } else {
                    $result["power"] = "";
                    $result["menu"] = "";
                }
                $outArr = $result;
            }
        }
        return $outArr;
    }

    /*
     * 检查是否登录
     */

    function checkAdminLogin() {

        $adminInfo = $this->loginAdminInfo();
        if ($adminInfo) {
            $outArr = $adminInfo;
        } else {
            //redirect(ADMIN_URL."&c=login");
            echo "<script>window.top.location.href='" . ADMIN_URL . "&c=login'</script>";
            exit;
        }
        return $outArr;
    }

    /*
     * 检查权限
     */

    function checkPower($paramArr = array()) {

        $flag = false;

        $adminInfo = $this->loginAdminInfo();

        $optionArr = array(
            "power" => "",
            "ajax" => 0,
        );
        if (!empty($paramArr)) {
            $optionArr = array_merge($optionArr, $paramArr);
        }
        extract($optionArr);
        if ($power != "") {
            if (strrpos($adminInfo["power"], "|" . $power . "|") !== false || $adminInfo["role_id"] == -1) {
                $flag = true;
            }
        }
        if ($flag == false) {
            throw new Exception("您没有此操作权限");
        }
    }

    public function addLog($is_force = 0, $ext_params = []) {

        $is_ajax = $this->input->is_ajax_request();
        $is_post = $this->input->post();
        if (($is_post && $is_ajax) || $is_force == 1) {
            $this->load->model("admin_operation_log_model");
            $params = [];
            //if ($this->input->post()) {
            $params["post"] = $this->input->post();
            //}
            if ($this->input->get()) {
                $params["get"] = $this->input->get();
            }
            if (!empty($_FILES)) {
                $params["files"] = $_FILES;
            }
            if (isset($_SERVER["HTTP_TOKEN"])) {
                $params["header"]["token"] = $_SERVER["HTTP_TOKEN"];
            }
            if (!empty($ext_params)) {
                $params["ext_params"] = $ext_params;
            }
            $d = $this->router->fetch_directory();
            $c = $this->router->fetch_class(); //获取控制器名
            $m = $this->router->fetch_method(); //获取方法名
            if (!empty($params)) {
                $action_name = $d . $c . "/" . $m;
                $this->admin_operation_log_model->addOperationLog(0, 0, $action_name, [
                    "params" => $params, "results" => $this->_return_result,
                ]);
            }
        }
    }

}
